# 自动获取构建时间和 Git 提交 ID
BUILD_DATE=$$(date +"%Y-%m-%d-%H:%M:%S")
GIT_COMMIT=$$(git rev-parse --short HEAD)

# 镜像前缀
IMAGE_PREFIX=registry.cn-hangzhou.aliyuncs.com/huthrc/dev
# 镜像名称
IMAGE_NAME=performance_api
# 架构
ARCH_AMD=amd
ARCH_ARM=arm

include .env

# 打包
build:
	mvn clean -DskipTests package
# 构建 arm镜像
docker-arm-build: build
	#删除旧标签
	docker rmi  ${IMAGE_PREFIX}:${IMAGE_NAME}_${ARCH_ARM}  || true
	docker rmi  ${IMAGE_NAME}_${ARCH_ARM}  || true

	docker build \
  --build-arg BUILD_DATE=${BUILD_DATE} \
  --build-arg GIT_COMMIT=${GIT_COMMIT} \
	--platform linux/arm64 \
	-t ${IMAGE_NAME}_${ARCH_ARM} .
	docker tag ${IMAGE_NAME}_${ARCH_ARM} ${IMAGE_PREFIX}:${IMAGE_NAME}_${ARCH_ARM}

# 运行arm镜像
docker-arm-run: docker-arm-build
	(export IMAGE_NAME=${IMAGE_PREFIX}:${IMAGE_NAME}_${ARCH_ARM}; docker compose  up)
# arm镜像构建并且运行
docker-arm-build-run: docker-arm-build docker-arm-run

# 构建amd镜像
docker-amd-build: build
	#删除旧标签
	docker rmi ${IMAGE_PREFIX}:${IMAGE_NAME}_${ARCH_AMD} || true
	docker rmi ${IMAGE_NAME}:${ARCH_AMD} || true

	docker build \
  --build-arg BUILD_DATE=${BUILD_DATE} \
  --build-arg GIT_COMMIT=${GIT_COMMIT} \
	--platform linux/amd64 \
	-t ${IMAGE_NAME}:${ARCH_AMD} .
	docker tag ${IMAGE_NAME}:${ARCH_AMD} ${IMAGE_PREFIX}:${IMAGE_NAME}_${ARCH_AMD}

# 运行amd镜像
docker-amd-run: docker-amd-build
	(export IMAGE_NAME=${IMAGE_PREFIX}:${IMAGE_NAME}_${ARCH_AMD}; docker compose  up)


# amd镜像构建并且运行
docker-amd-build-run: docker-amd-build docker-amd-run

# 构建amd和arm镜像
docker-build: docker-amd-build docker-arm-build


# amd和arm镜像构建并且运行
docker-build-run: docker-build docker-arm-run docker-amd-run

# push 镜像
docker-push: docker-build
	docker push ${IMAGE_PREFIX}:${IMAGE_NAME}_${ARCH_AMD}
	docker push ${IMAGE_PREFIX}:${IMAGE_NAME}_${ARCH_ARM}

# 查看镜像架构
docker-arch:
	docker inspect --format='{{.Architecture}}' ${IMAGE_PREFIX}:${IMAGE_NAME}_${ARCH_AMD}
	docker inspect --format='{{.Architecture}}' ${IMAGE_PREFIX}:${IMAGE_NAME}_${ARCH_ARM}

# 查看插件的全部目标
help:
	mvn help:describe -Dplugin=org.springframework.boot:spring-boot-maven-plugin -Ddetail=true

# 运行程序
run:
	set -a && source .env && set +a && 	mvn spring-boot:run